﻿using System;
using System.Diagnostics;

namespace CSLib.Utils
{
	/// <summary>
	/// 時間計測用のユーティリティクラス。
	/// IDisposableを実装しているため、usingで時間計測が可能。
	/// </summary>
	public class Measure : IDisposable
	{
		private static NLog.Logger log = NLog.LogManager.GetCurrentClassLogger();

		private Stopwatch _sw;
		private string _message;

		/// <summary>
		/// Constructor.
		/// </summary>
		public Measure()
		{
			_sw = Stopwatch.StartNew();
		}

		/// <summary>
		/// Constructor.
		/// </summary>
		/// <param name="message">表示メッセージ</param>
		public Measure(string message)
		{
			_sw = Stopwatch.StartNew();
			_message = message;
		}

		/// <summary>
		/// 計測時間の表示.
		/// </summary>
		void IDisposable.Dispose()
		{
			_sw.Stop();
			if (_message == null)
			{
				log.Info("Elapsed time = {0}ms.", _sw.ElapsedMilliseconds);
			}
			else
			{
				log.Info("{0} : Elapsed time = {1}ms.", _message, _sw.ElapsedMilliseconds);
			}
		}
	}
}
